﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Job.Entity;
using Job.Web.Framework;

namespace Job.Logic.Common
{
    public class MemoryDictionary
    {
        private static Hashtable HashCommonDictionary = null;    //保存一级的字典
        private static Hashtable HashAddrDictionary = null;      //保存地区的字典
        private static Hashtable HashSpecialtyDictionary = null; //保存专业的字典
        private static Hashtable HashIndustryDictionary = null;  //保存行业的字典
        private static Hashtable HashPositionDictionary = null;  //保存职业类别的字典
        private static Hashtable HashITAbilityDictionary = null; //保存IT技能的字典
        private static Hashtable HashCertificateDictionary = null; //保存证书名称的字典构

        public static void init()
        {
            LoadCommonDictionary();
            LoadAddrDictionary();
            LoadIndustryDictionary();
            LoadSpecialtyDictionary();
            LoadPositionClassDictionary();
        }


        #region 对一般字典的操作
        public static void LoadCommonDictionary()
        {
            try
            {
                DictionaryLogic logic = new DictionaryLogic();
                IList<JobCommonDictionary> list = logic.GetAll();
                Hashtable hash = new Hashtable();

                foreach (JobCommonDictionary dict in list)
                {
                    hash.Add(dict.DictId.ToString(), dict);
                }

                //Hashtable tmp = HashCommonDictionary;
                HashCommonDictionary = hash;
                //tmp.Clear();
            }
            catch (Exception ex)
            {
                Logger.WriteLog("加载一般数据字典到内存失败：" + ex.Message);
            }
        }
        public static string GetCommonDictName(int id)
        {
            if ( id <= 0 ) return "";
            return GetCommonDictName(id.ToString());
        }
        public static string GetCommonDictName(string id)
        {
            if ( null == id || id.Equals("") ) return "";
            if (null == HashCommonDictionary) LoadCommonDictionary();

            if (HashCommonDictionary.ContainsKey(id))
            {
                JobCommonDictionary dict = (JobCommonDictionary)HashCommonDictionary[id];
                if (null == dict) return "";
                return dict.DictName;
            }
            else
            {
                return "";
            }
        }
        #endregion

        #region 对地区字典的操作
        public static void LoadAddrDictionary()
        {
            try 
            {
                DictionaryLogic logic = new DictionaryLogic();
                IList<JobLevelDictionary> list = logic.FindAllByClass(
                    Constants.GetDictionaryCode(Constants.DictionaryType.ADDR_CITY));
                Hashtable hash = new Hashtable();
                
                foreach(JobLevelDictionary dict in list)
                {
                    hash.Add(dict.SubCode,dict);
                }

                //Hashtable tmp = HashAddrDictionary;
                HashAddrDictionary = hash;
                //tmp.Clear();
            }
            catch(Exception ex)
            {
                Console.WriteLine("加载地区字典到内存失败：" + ex.Message);
                Logger.WriteLog("加载地区字典到内存失败：" + ex.Message);
            }
        }
        //只返回地区的名称
        public static string GetAddrName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashAddrDictionary) LoadAddrDictionary();
            if ( HashAddrDictionary.ContainsKey(code) )
            {
                JobLevelDictionary dict = (JobLevelDictionary)HashAddrDictionary[code];
                if (null == dict) return "";
                return dict.DictName;
            }
            else
            {
                return "";
            }
        }     
        //返回地区名称，返回的名称由 省＋地区组成
        public static string GetFullAddrName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashAddrDictionary) LoadAddrDictionary();
            if ( HashAddrDictionary.ContainsKey(code) )
            {
                string name;
                JobLevelDictionary dict = (JobLevelDictionary)HashAddrDictionary[code];
                if (null == dict) return "";
                name = dict.DictName;
                if ( code.Length <= 2 ) return name;

                string father = code.Substring(0,2);
                if (HashAddrDictionary.ContainsKey(father))
                {
                    dict = (JobLevelDictionary)HashAddrDictionary[father];
                    if ( null == dict ) return name;
                    name = dict.DictName + " - " + name;
                    return name;
                }
                else
                {
                    return name;
                }

            }
            else
            {
                return "";
            }
        } 
        #endregion

        #region 对专业字典的操作
        public static void LoadSpecialtyDictionary()
        {
            try 
            {
                DictionaryLogic logic = new DictionaryLogic();
                IList<JobLevelDictionary> list = logic.FindAllByClass(
                    Constants.GetDictionaryCode(Constants.DictionaryType.SPECIALTY_CLASS));
                Hashtable hash = new Hashtable();
                
                foreach(JobLevelDictionary dict in list)
                {
                    hash.Add(dict.SubCode,dict);
                }

                //Hashtable tmp = HashSpecialtyDictionary;
                HashSpecialtyDictionary = hash;
                //tmp.Clear();
            }
            catch(Exception ex)
            {
                Console.WriteLine("加载专业字典到内存失败：" + ex.Message);
                Logger.WriteLog("加载专业字典到内存失败：" + ex.Message);
            }
        }
        //只返回专业的名称
        public static string GetSpecialtyName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashSpecialtyDictionary) LoadSpecialtyDictionary();
            if ( HashSpecialtyDictionary.ContainsKey(code) )
            {
                JobLevelDictionary dict = (JobLevelDictionary)HashSpecialtyDictionary[code];
                if (null == dict) return "";
                return dict.DictName;
            }
            else
            {
                return "";
            }
        }     
        //返回专业名称，返回的名称由 大类名称＋小类名称组成
        public static string GetFullSpecialtyName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashSpecialtyDictionary) LoadSpecialtyDictionary();
            if ( HashSpecialtyDictionary.ContainsKey(code) )
            {
                string name;
                JobLevelDictionary dict = (JobLevelDictionary)HashSpecialtyDictionary[code];
                if (null == dict) return "";
                name = dict.DictName;
                if ( code.Length <= 2 ) return name;

                string father = code.Substring(0,2);
                if (HashSpecialtyDictionary.ContainsKey(father))
                {
                    dict = (JobLevelDictionary)HashSpecialtyDictionary[father];
                    if ( null == dict ) return name;
                    name = dict.DictName + " - " + name;
                    return name;
                }
                else
                {
                    return name;
                }

            }
            else
            {
                return "";
            }
        } 
        #endregion
                   
        #region 对行业字典的操作
        public static void LoadIndustryDictionary()
        {
            try 
            {
                DictionaryLogic logic = new DictionaryLogic();
                IList<JobLevelDictionary> list = logic.FindAllByClass(
                    Constants.GetDictionaryCode(Constants.DictionaryType.INDUSTRY_CLASS));
                Hashtable hash = new Hashtable();
                
                foreach(JobLevelDictionary dict in list)
                {
                    hash.Add(dict.SubCode,dict);
                }

                //Hashtable tmp = HashIndustryDictionary;
                HashIndustryDictionary = hash;
                //tmp.Clear();
            }
            catch(Exception ex)
            {
                Console.WriteLine("加载行业字典到内存失败：" + ex.Message);
                Logger.WriteLog("加载行业字典到内存失败：" + ex.Message);
            }
        }
        //只返回行业的名称
        public static string GetIndustryName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashIndustryDictionary) LoadIndustryDictionary();
            if ( HashIndustryDictionary.ContainsKey(code) )
            {
                JobLevelDictionary dict = (JobLevelDictionary)HashIndustryDictionary[code];
                if (null == dict) return "";
                return dict.DictName;
            }
            else
            {
                return "";
            }
        }     
        //返回行业名称，返回的名称由 大类名称＋小类名称组成
        public static string GetFullIndustryName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashIndustryDictionary) LoadIndustryDictionary();
            if ( HashIndustryDictionary.ContainsKey(code) )
            {
                string name;
                JobLevelDictionary dict = (JobLevelDictionary)HashIndustryDictionary[code];
                if (null == dict) return "";
                name = dict.DictName;
                if ( code.Length <= 2 ) return name;

                string father = code.Substring(0,2);
                if ( HashIndustryDictionary.ContainsKey(father) )
                {
                    dict = (JobLevelDictionary)HashIndustryDictionary[father];
                    if ( null == dict ) return name;
                    name = dict.DictName + " - " + name;
                    return name;
                }
                else
                {
                    return name;
                }

            }
            else
            {
                return "";
            }
        } 
        #endregion
                             
        #region 对职业类别字典的操作
        public static void LoadPositionClassDictionary()
        {
            try 
            {
                DictionaryLogic logic = new DictionaryLogic();
                IList<JobLevelDictionary> list = logic.FindAllByClass(
                    Constants.GetDictionaryCode(Constants.DictionaryType.POSITION_CLASS));
                Hashtable hash = new Hashtable();
                
                foreach(JobLevelDictionary dict in list)
                {
                    hash.Add(dict.SubCode,dict);
                }

                //Hashtable tmp = HashPositionDictionary;
                HashPositionDictionary = hash;
                //tmp.Clear();
            }
            catch(Exception ex)
            {
                Console.WriteLine("加载职业类别字典到内存失败：" + ex.Message);
                Logger.WriteLog("加载职业类别字典到内存失败：" + ex.Message);
            }
        }
        //只返回职业类别的名称
        public static string GetPositionClassName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashPositionDictionary) LoadPositionClassDictionary();
            if ( HashPositionDictionary.ContainsKey(code) )
            {
                JobLevelDictionary dict = (JobLevelDictionary)HashPositionDictionary[code];
                if (null == dict) return "";
                return dict.DictName;
            }
            else
            {
                return "";
            }
        }     
        //返回职业类别名称，返回的名称由 大类名称＋小类名称组成
        public static string GetFullPositionClassName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashPositionDictionary) LoadPositionClassDictionary();
            if ( HashPositionDictionary.ContainsKey(code) )
            {
                string name;
                JobLevelDictionary dict = (JobLevelDictionary)HashPositionDictionary[code];
                if (null == dict) return "";
                name = dict.DictName;
                if ( code.Length <= 2 ) return name;

                string father = code.Substring(0,2);
                if (HashPositionDictionary.ContainsKey(father))
                {
                    dict = (JobLevelDictionary)HashPositionDictionary[father];
                    if ( null == dict ) return name;
                    name = dict.DictName + " - " + name;
                    return name;
                }
                else
                {
                    return name;
                }

            }
            else
            {
                return "";
            }
        } 
        #endregion            

        #region 对IT技能字典的操作
        public static void LoadItAbilityDictionary()
        {
            try 
            {
                DictionaryLogic logic = new DictionaryLogic();
                IList<JobLevelDictionary> list = logic.FindAllByClass(
                    Constants.GetDictionaryCode(Constants.DictionaryType.IT_ABILITY));
                Hashtable hash = new Hashtable();
                
                foreach(JobLevelDictionary dict in list)
                {
                    hash.Add(dict.SubCode,dict);
                }

                //Hashtable tmp = HashITAbilityDictionary;
                HashITAbilityDictionary = hash;
                //tmp.Clear();
            }
            catch(Exception ex)
            {
                Console.WriteLine("加载IT技能字典到内存失败：" + ex.Message);
                Logger.WriteLog("加载IT技能字典到内存失败：" + ex.Message);
            }
        }
        //只返回IT技能的名称
        public static string GetItAbilityName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashITAbilityDictionary) LoadItAbilityDictionary();
            if ( HashITAbilityDictionary.ContainsKey(code) )
            {
                JobLevelDictionary dict = (JobLevelDictionary)HashITAbilityDictionary[code];
                if (null == dict) return "";
                return dict.DictName;
            }
            else
            {
                return "";
            }
        }     
        //返回IT技能名称，返回的名称由 大类名称＋小类名称组成
        public static string GetFullItAbilityName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashITAbilityDictionary) LoadItAbilityDictionary();
            if ( HashITAbilityDictionary.ContainsKey(code) )
            {
                string name;
                JobLevelDictionary dict = (JobLevelDictionary)HashITAbilityDictionary[code];
                if (null == dict) return "";
                name = dict.DictName;
                if ( code.Length <= 2 ) return name;

                string father = code.Substring(0,2);
                if (HashITAbilityDictionary.ContainsKey(father))
                {
                    dict = (JobLevelDictionary)HashITAbilityDictionary[father];
                    if ( null == dict ) return name;
                    name = dict.DictName + " - " + name;
                    return name;
                }
                else
                {
                    return name;
                }

            }
            else
            {
                return "";
            }
        } 
        #endregion            

        #region 对证书名称字典的操作
        public static void LoadCertificateNameDictionary()
        {
            try 
            {
                DictionaryLogic logic = new DictionaryLogic();
                IList<JobLevelDictionary> list = logic.FindAllByClass(
                    Constants.GetDictionaryCode(Constants.DictionaryType.CERTIFICATE_NAME));
                Hashtable hash = new Hashtable();
                
                foreach(JobLevelDictionary dict in list)
                {
                    hash.Add(dict.SubCode,dict);
                }

                //Hashtable tmp = HashCertificateDictionary;
                HashCertificateDictionary = hash;
                //tmp.Clear();
            }
            catch(Exception ex)
            {
                Console.WriteLine("加载证书名称字典到内存失败：" + ex.Message);
                Logger.WriteLog("加载证书名称字典到内存失败：" + ex.Message);
            }
        }
        //只返回证书名称的名称
        public static string GetCertificateName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashCertificateDictionary) LoadCertificateNameDictionary();
            if (HashCertificateDictionary.ContainsKey(code))
            {
                JobLevelDictionary dict = (JobLevelDictionary)HashCertificateDictionary[code];
                if (null == dict) return "";
                return dict.DictName;
            }
            else
            {
                return "";
            }
        }     
        //返回证书名称名称，返回的名称由 大类名称＋小类名称组成
        public static string GetFullCertificateName(string code)
        {
            if ( null == code || code.Equals("") ) return "";
            if (null == HashCertificateDictionary) LoadCertificateNameDictionary();
            if (HashCertificateDictionary.ContainsKey(code))
            {
                string name;
                JobLevelDictionary dict = (JobLevelDictionary)HashCertificateDictionary[code];
                if (null == dict) return "";
                name = dict.DictName;
                if ( code.Length <= 2 ) return name;

                string father = code.Substring(0,2);
                if (HashCertificateDictionary.ContainsKey(father))
                {
                    dict = (JobLevelDictionary)HashCertificateDictionary[father];
                    if ( null == dict ) return name;
                    name = dict.DictName + " - " + name;
                    return name;
                }
                else
                {
                    return name;
                }

            }
            else
            {
                return "";
            }
        } 
        #endregion            

    }
}
